﻿using System.Collections.Generic;
using System.Web.Security;
using LightWeb.DomainModel;
using LightWeb.Infrastructure.Repositories;

namespace LightWeb.Application.WebSecurity
{
    public class SecurityService : IMembershipProviderRepository, IRoleProviderRepository
    {
        DomainModel.Security.Service RepositoryService = new DomainModel.Security.Service();

        #region IMembershipProviderRepository
        public void CreateUser(User user)
        {
            RepositoryService.CreateUser(user);
        }

        public bool ValidateUser(string username, string password)
        {
            return RepositoryService.ValidateUser(new User { LoginName = username, Password = password });
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new System.NotImplementedException();
        }

        public bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new System.NotImplementedException();
        }

        public bool DelteUser(string username, bool deleteAllRelatedData)
        {
            throw new System.NotImplementedException();
        }

        public PagerContent<MembershipUser> FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize)
        {
            throw new System.NotImplementedException();
        }

        public PagerContent<MembershipUser> FindUsersByName(string usernameToMatch, int pageIndex, int pageSize)
        {
            throw new System.NotImplementedException();
        }

        public PagerContent<MembershipUser> GetAllUsers(int pageIndex, int pageSize)
        {
            throw new System.NotImplementedException();
        }

        public int GetNumberOfUsersOnline()
        {
            throw new System.NotImplementedException();
        }

        public MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new System.NotImplementedException();
        }

        public string GetPassword(string username, string answer)
        {
            throw new System.NotImplementedException();
        }

        public MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new System.NotImplementedException();
        }

        public string GetUserNameByEmail(string email)
        {
            throw new System.NotImplementedException();
        }

        public string ResetPassword(string username, string answer)
        {
            throw new System.NotImplementedException();
        }

        public bool UnlockUser(string userName)
        {
            throw new System.NotImplementedException();
        }

        public void UpdateUser(MembershipUser user)
        {
            throw new System.NotImplementedException();
        }

        #endregion

        #region IRoleProviderRepository
        public IList<Role> GetAllRoles()
        {
            return RepositoryService.GetAllRoles();
        }

        public IList<Role> GetRolesForUser(string username)
        {
            return RepositoryService.GetRolesForUser(username);
        }

        public IList<User> GetUsersInRole(string roleName)
        {
            return RepositoryService.GetUsersInRole(roleName);
        }

        public bool IsUserInRole(string username, string roleName)
        {
            return RepositoryService.IsUserInRole(username, roleName);
        }

        public bool RoleExists(string roleName)
        {
            return RepositoryService.RoleExists(roleName);
        }

        public bool RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            return RepositoryService.RemoveUsersFromRoles(usernames, roleNames);
        }

        public void CreateRole(string roleName)
        {
            RepositoryService.CreateRole(roleName);
        }

        public bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            return RepositoryService.DeleteRole(roleName, throwOnPopulatedRole);
        }

        public IList<Role> GetUsersInRole(string roleName, string usernameToMatch)
        {
            return RepositoryService.GetUsersInRole(roleName, usernameToMatch);
        }

        public void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            RepositoryService.AddUsersToRoles(usernames, roleNames);
        }

        #endregion
    }

    public interface IMembershipProviderRepository
    {
        void CreateUser(DomainModel.User user);

        bool ValidateUser(string username, string password);

        bool ChangePassword(string username, string oldPassword, string newPassword);

        bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer);

        bool DelteUser(string username, bool deleteAllRelatedData);

        PagerContent<MembershipUser> FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize);

        PagerContent<MembershipUser> FindUsersByName(string usernameToMatch, int pageIndex, int pageSize);

        PagerContent<MembershipUser> GetAllUsers(int pageIndex, int pageSize);

        int GetNumberOfUsersOnline();

        MembershipUser GetUser(string username, bool userIsOnline);

        string GetPassword(string username, string answer);

        MembershipUser GetUser(object providerUserKey, bool userIsOnline);

        string GetUserNameByEmail(string email);

        string ResetPassword(string username, string answer);

        bool UnlockUser(string userName);

        void UpdateUser(MembershipUser user);
    }

    public interface IRoleProviderRepository
    {
        IList<Role> GetAllRoles();

        IList<Role> GetRolesForUser(string username);

        IList<User> GetUsersInRole(string roleName);

        bool IsUserInRole(string username, string roleName);

        bool RoleExists(string roleName);

        bool RemoveUsersFromRoles(string[] usernames, string[] roleNames);

        void CreateRole(string roleName);

        bool DeleteRole(string roleName, bool throwOnPopulatedRole);

        IList<Role> GetUsersInRole(string roleName, string usernameToMatch);

        void AddUsersToRoles(string[] usernames, string[] roleNames);
    }
}
